package com.sync.utils.project.demo.service.impl;

import com.sync.utils.common.base.service.impl.BaseServiceImpl;
import com.sync.utils.common.enums.URLEnum;
import com.sync.utils.framework.okHttp.OkHttpUtils;
import com.sync.utils.project.demo.mapper.AsyncDataMapper;
import com.sync.utils.project.demo.pojo.AsyncData;
import com.sync.utils.project.demo.service.AsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2022/2/25 10:42
 */
@Service
public class AsyncServiceImpl extends BaseServiceImpl<AsyncDataMapper, AsyncData> implements AsyncService {

    private final static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);

    @Autowired
    private AsyncDataMapper asyncDataMapper;

    @Override
    @Async("asyncServiceExecutor")
    public void executeAsync(List<AsyncData> asyncDataList, CountDownLatch countDownLatch) {
        try{

            logger.warn("start executeAsync,{}",countDownLatch);
            //异步线程要做的事情
            for (AsyncData asyncData : asyncDataList) {
                asyncDataMapper.insert(asyncData);
            }
            logger.warn("end executeAsync");
        }finally {
            countDownLatch.countDown();// 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放
        }
    }

    @Override
    public List<Map> getEarthquakeReport() {

        //String result = OkHttpUtils.builder().url("https://www.ceic.ac.cn/ajax/speedsearch?num=2&&page=1&&callback=jQuery18009238280817206295_1703666053311&_=1703666076391")
        //        // 有参数的话添加参数，可多个
        //        .get()
        //        .sync();

        String url ="https://www.ceic.ac.cn/ajax/speedsearch?num=2&&page=1&&callback=jQuery18009238280817206295_1703666053311&_=1703666076391";
        String result = null;
        try {
            result = httpAPIService.doGet(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("result:"+result);
        return null;
    }


}
